<?php

namespace app\modules\Agreement\models;

use Yii;
use app\modules\Basic\models\Customer;
use app\modules\Manage\models\User;

/**
 * This is the model class for table "ai_agreement".
 *
 * @property int $id
 * @property string $code 合同编号
 * @property int $customer_id 客户ID
 * @property int $type 1 内贸 2外贸 
 * @property int $order_type 1 首签 2维修 
 * @property string $sale_currency 合同币种
 * @property string $sale_price 合同金额
 * @property string $payment_type 付款方式
 * @property string $payment_date 货期
 * @property string $warranty 质保期
 * @property string $exemption 免表
 * @property string $trade_company 外贸公司
 * @property string $trade_contract 外贸合同号
 * @property string $invoice_no 开票号
 * @property string $trade_main 签约主体
 * @property string $send_date 发货日期
 * @property string $install_date 安装日期
 * @property string $remarks 备注
 * @property int $user_id 创建人ID
 * @property int $status 1提交 2删除
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class Agreement extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'ai_agreement';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['code'], 'required'],
            [['customer_id', 'type', 'order_type', 'user_id', 'status','con_num'], 'integer'],
            [['sale_price'], 'number'],
            [['send_date', 'install_date', 'created_at', 'updated_at'], 'safe'],
            [['code'], 'string', 'max' => 255],
            [['sale_currency'], 'string', 'max' => 10],
            [['payment_type', 'payment_date', 'warranty', 'exemption', 'trade_company', 'trade_contract', 'invoice_no', 'trade_main'], 'string', 'max' => 128],
            [['remarks'], 'string', 'max' => 522],
        ];
    }

    //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){

            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
        return parent::beforeSave($insert);

    }//}}}

    //  获取客户信息
    public function getCustomer(){
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

    //获取创建人
    public function getUserName(){
        $user = User::findOne($this->user_id);
        return $user ? $user->name : ' ';
    }

    //获取货品名称
    public function getGoods(){
        return $this->hasMany(Goods::className(), ['agreement_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'code' => '合同编号',
            'customer_id' => '客户',
            'type' => '合同性质',
            'order_type' => '订单类型',
            'sale_currency' => '币种',
            'sale_price' => '签订金额',
            'payment_type' => '付款方式',
            'payment_date' => '货期',
            'warranty' => '质保期',
            'exemption' => '免表',
            'trade_company' => '外贸公司',
            'trade_contract' => '外贸合同号',
            'invoice_no' => '开票号',
            'trade_main' => '签约主体',
            'send_date' => '发货日期',
            'install_date' => '安装日期',
            'remarks' => '备注',
            'user_id' => '创建人',
            'status' => '合同状态',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }
}
